<template>
	<view style="background: #ededed;">
		<view class="container" v-if="showCalendar">
			<!-- 插入模式 -->
			<uni-calendar class="uni-calendar--hook" :selected="info.selected" :showMonth="false" @change="change"
				@monthSwitch="monthSwitch" />
		</view>
		<!-- 最新活动 -->
		<view class="enlightenment-list">
			<view class="enlightenment" v-for="(item, index) in activityList" :key="index">
				<view class="enlightenment_nav">
					<view class="box" v-if="item.status==1" v-bind:style="{ background: '#00aa00' }">
						<up-text color="#ffffff" size=15 align="center" text="报名中"></up-text>
					</view>
					<view class="box" v-else-if="item.status==2" v-bind:style="{ background: '#e70000' }">
						<up-text color="#ffffff" size=15 align="center" text="进行中"></up-text>
					</view>
					<view class="box" v-if="item.status==0" v-bind:style="{ background: '#b8b8b8' }">
						<up-text color="#ffffff" size=15 align="center" text="已结束"></up-text>
					</view>
					<view style="margin-top: 20rpx;margin-right: 20rpx;width: 40%;">
						<up-image style="margin-top: 10rpx;" width="220rpx" height="300rpx" radius="20rpx"
							:show-loading="true" :src="item.url"></up-image>
					</view>
					<view style="margin-top: 20rpx; margin-right: 20rpx;width: 80vw;">
						<view class="spacer" style="height: 20rpx;"></view>
						<up-text :bold="true" size=18 :text="item.name"></up-text>
						<view class="spacer" style="height: 15rpx;"></view>
						<up-text size=10 :text="`活动时间:${item.time}`"></up-text>
						<view class="spacer" style="height: 30rpx;"></view>
						<up-text :text="item.introduction"></up-text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * 获取任意时间
	 */
	function getDate(date, AddDayCount = 0) {
		if (!date) {
			date = new Date();
		}
		if (typeof date !== 'object') {
			date = date.replace(/-/g, '/');
		}
		const dd = new Date(date);

		dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期

		const y = dd.getFullYear();
		const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期，不足10补0
		const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
		return {
			fullDate: y + '-' + m + '-' + d,
			year: y,
			month: m,
			date: d,
			day: dd.getDay()
		};
	}
	export default {
		components: {},
		data() {
			return {
				showCalendar: false,
				info: {
					lunar: true,
					range: true,
					insert: false,
					selected: []
				},
				// 最新活动
				activityList: [{
					id:1,
					url: "https://img.zcool.cn/community/011f075ebd771aa801214814eab377.jpg@1280w_1l_2o_100sh.jpg",
					name: "全民读好书",
					status: 0,
					time: "2024年6月21日",
					introduction: "11位主持人和特邀连线嘉宾一起直播读书带货，给有需要的孩子捐赠书本。"
				}, {
					id:1,
					url: "https://img.zcool.cn/community/01d8965c739ea7a801213f26d3604d.png@1280w_1l_2o_100sh.png",
					name: "荆棘鸟书会",
					status: 2,
					time: "2024年6月21日",
					introduction: "一个人在美丽纷繁的野外行走，因为自我中心的限制，无法看到周身瑰丽景色。"
				}, {
					id:1,
					url: "https://ts1.cn.mm.bing.net/th/id/R-C.d65fc2bdb85e811df3b7d55de9949bb8?rik=rzgNwpd3cIQTzQ&riu=http%3a%2f%2fimg95.699pic.com%2fdesgin_photo%2f40100%2f2620_detail.jpg!detail860%2ffw%2f820%2fcrop%2f0x1309a0a0%2fquality%2f90&ehk=wWophK2LKW%2blLqktL9WRIOmAmSy3yqVybPM16lSax48%3d&risl=&pid=ImgRaw&r=0",
					name: "创意世界读书日",
					status: 1,
					time: "2024年6月21日",
					introduction: "读一本好书",
				}],
			};
		},
		onReady() {
			this.$nextTick(() => {
				this.showCalendar = true;
			});
			// TODO 模拟请求异步同步数据
			setTimeout(() => {
				this.info.selected = [{
						date: getDate(new Date(), -3).fullDate,
					},
					{
						date: getDate(new Date(), -2).fullDate,
					},
					{
						date: getDate(new Date(), -1).fullDate,
					}
				];
			}, 2000);
		},
		methods: {
			change(e) {
				console.log('change 返回:', e);
				// 模拟动态打卡
				if (this.info.selected.length > 5) return;
				this.info.selected.push({
					date: e.fulldate,
					info: '打卡'
				});
			},
			confirm(e) {
				console.log('confirm 返回:', e);
			},
			monthSwitch(e) {
				console.log('monthSwitchs 返回:', e);
			}
		}
	};
</script>

<style lang="scss">
	.example-body {
		display: flex;
		flex-direction: row;
	}

	.spacer {
		background-color: transparent;
	}

	.calendar-button {
		flex: 1;
		font-weight: bold;
		font-size: 32rpx;
	}

	.enlightenment-list {
		padding: 20rpx;
		margin: 20rpx;
	}

	.enlightenment {
		width: 90vw;
		height: 340rpx;
		background: #ffffff;
		border-radius: 20rpx;
		margin-top: 30rpx;
	}

	.enlightenment_nav {
		display: flex;
		margin-top: 30rpx;
		margin-left: 20rpx;
	}

	.box {
		position: absolute;
		transform: translate(550%, 100%);
		width: 100rpx;
		height: 40rpx;
		border-radius: 0px 50rpx 50rpx 0rpx; /* border-radius的值为容器宽度的一半 */
	}
</style>